/**
 ******************************************************************************
 * @file      chkfrm.c
 * @brief     C Source file of chkfrm.c.
 * @details   This file including all API functions's implement of chkfrm.c.
 * @copyright
 ******************************************************************************
 */

/*-----------------------------------------------------------------------------
 Section: Includes
 ----------------------------------------------------------------------------*/
#include <stdio.h>

#include "../GDW1376_2.h"
#include "lib.h"

/*-----------------------------------------------------------------------------
 Section: Type Definitions
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Constant Definitions
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Global Variables
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Local Variables
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Local Function Prototypes
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Global Function Prototypes
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Function Definitions
 ----------------------------------------------------------------------------*/
/**
 ******************************************************************************
 * @brief   检查1376.2报文是否合法
 * @param[in]  *pin         : 输入报文
 * @param[in]   len         : 输入报文长度
 *
 * @retval   0  : 报文合法
 * @retval  -1  : 输入参数非法
 * @retval  -2  : 报文头0x68
 * @retval  -3  : 输入报文长度无效
 * @retval  -4  : 报文尾0x16
 * @retval  -5  : 报文cs错误
 ******************************************************************************
 */
int chkfrm_GDW1376_2(const unsigned char *pin, unsigned int len) {
  unsigned int packlen = 0;

  if ((pin == NULL) || (len < 11))  // 0x68 + len(2) + R(6) +...+ cs + 0x16
  {
    return -ERR_INPUT;
  }

  if (pin[0] != 0x68) {
    return -ERR_CHKFRM_0x68;
  }

  packlen = ((unsigned int)pin[1]) | (((unsigned int)pin[2]) << 8);

  if (packlen != len) {
    return -ERR_CHKFRM_LEN;
  }

  if (pin[len - 1] != 0x16) {
    return -ERR_CHKFRM_0x16;
  }

  if (get_cs(pin + 3, len - 5) != pin[len - 2]) {
    return -ERR_CHKFRM_cs;
  }

  return ERR_NONE;
}

/*-------------------------------chkfrm.c------------------------------------*/
